package com.dream21th.common.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public final class Dates {

    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss.S";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YMHS = "yyyy-MM-dd HH:mm:ss";
    public static final String HMS = "HHmmss";


    public static String getHMSTime() {
        return LocalTime.now().format(DateTimeFormatter.ofPattern(HMS));
    }

    public static String getYMDDate() {
        return LocalDate.now().format(DateTimeFormatter.ofPattern(YYYYMMDD));
    }

    /**
     * 根据期限计算贷款到期日
     * @return
     */
    public static String loanDeadline(int term) {
        return LocalDate.now().plusMonths(term).minusDays(1).format(DateTimeFormatter.ofPattern(YYYY_MM_DD));
    }

    public static boolean isAfterNow(String t){
        return LocalDateTime.parse(t,DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS)).isAfter(LocalDateTime.now());
    }

    /**
     * 得到当前时间,格式为yyyyMMddHHmmss
     *
     * @return
     */
    public static String getyyyyMMddHHmmssCurDate() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS));
    }

    public static String getYMHSCurDate() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(YMHS));
    }

    public static String getyyyyMMddCurDate() {
        return LocalDate.now().format(DateTimeFormatter.ofPattern(YYYY_MM_DD));
    }

    public static String convertBySelf(String str, String self, String dist) {
        return LocalDateTime.parse(str, DateTimeFormatter.ofPattern(self)).format(DateTimeFormatter.ofPattern(dist));
    }

    public static String convertLocalDateToString(LocalDate date) {
        return date.format(DateTimeFormatter.ofPattern(YYYY_MM_DD));
    }

    /**
     * String日期字符串 转 LocalDateTime
     *
     * @param timeStr
     * @param pattern
     * @return
     */
    public static LocalDateTime dateStrToLocalDateTime(String timeStr, String pattern) {
        return LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * localDateTime 转 date
     *
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }


    public static int getAgeByIdNo(String idNo) {
        if (idNo == null || idNo.length() != 18) {
            return 0;
        }
        LocalDate bir = LocalDate.parse(idNo.substring(6, 14), DateTimeFormatter.ofPattern(YYYYMMDD));
        return bir.until(LocalDate.now()).getYears();
    }

    public static boolean checkTimeLimit(String lastTime, int days) {
        return LocalDateTime.now().minusDays(days).isAfter(LocalDateTime.parse(lastTime, DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS)));
    }
}
